import { createRouter, createWebHistory } from 'vue-router'

const routers=JSON.parse(localStorage.getItem('router') as string)
let renders=(arr:any)=>{
  let add:any=[]
  arr&&arr.forEach((item:any)=>{
    if(item.child===1){
      add.push({
        path:item.path,
        name:item.name,
        id:item.id,
        component:()=>import('../views/'+item.component),
        children:renders(item.children)
      })
    }else{
      add.push({
        path:item.path,
        name:item.name,
        id:item.id,
        component:()=>import('../views/'+item.component)
      })
    }
  })
  return add
}
export const routes=renders(routers)
// export const routes=[
//   {
//     path:'/manage/homepage',
//     name:'首页',
//     id:1,
//     component:()=>import('../views/Home/Homepage.vue')
//   },
//   {
//     path:'/manage/largescreen',
//     name:'大屏管理',
//     id:2,
//     component:()=>import('../views/Home/Largescreen.vue'),
//     children:[
//       {
//         path:'/manage/largescreen/employment',
//         name:'就业态势',
//         id:5,
//         component:()=>import('../views/Home/Manage/Employment.vue')
//       },
//       {
//         path:'/manage/largescreen/intelligent',
//         name:'智能检测',
//         id:6,
//         component:()=>import('../views/Home/Manage/Intelligent.vue')
//       },
//       {
//         path:'/manage/largescreen/security',
//         name:'安全态势',
//         id:7,
//         component:()=>import('../views/Home/Manage/Security.vue')
//       },
//       {
//         path:'/manage/largescreen/equipment',
//         name:'设备运维',
//         id:8,
//         component:()=>import('../views/Home/Manage/Equipment.vue')
//       }
//     ]
//   },
//   {
//     path:'/manage/userpage',
//     name:'用户管理',
//     id:3,
//     component:()=>import('../views/Home/Userpage.vue')
//   },
//   {
//     path:'/manage/statuspage',
//     name:'状态管理',
//     id:4,
//     component:()=>import('../views/Home/Statuspage.vue')
//   }
// ]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:'/manager'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/manager',
      name: 'manager',
      component: () => import('../views/Manager.vue'),
      children:routes
    },
     {
      path: '/:pathMatch(.*)*',
      component: () => import('../views/Error.vue')
    }
  ]
})

// 路由前置守卫
router.beforeEach((to,from,next)=>{
  const token=localStorage.getItem('token')
  if(to.path!=='/login'&&!token){
    localStorage.setItem('token',to.fullPath)
    next({path:'/login'})
  }else{
    next()
  }
})


export default router
